package com.zhkj.txg.module.member.adapter

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.txg.R
import com.zhkj.txg.module.member.entity.InterestChildEntity
import com.zhkj.txg.module.member.entity.InterestEntity
import com.zhkj.txg.utils.ImageUtils

class InterestHorizontalAdapter : BaseMultiItemQuickAdapter<InterestEntity, InterestViewHolder>() {
    init {
        addItemType(0, R.layout.item_interest_horizontal_rv)
        addItemType(1, R.layout.item_interest_footer)
        addChildClickViewIds(R.id.tvInterestFooter)
    }

    override fun onItemViewHolderCreated(viewHolder: InterestViewHolder, viewType: Int) {
        if (viewType == 0) with(viewHolder.getView<RecyclerView>(R.id.rvInterest)) {
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
            adapter = viewHolder.adapter
        }
    }

    override fun convert(helper: InterestViewHolder, item: InterestEntity) {
        when (item.itemType) {
            0 -> {
                helper.setGone(R.id.tvInterestTitle, item.title.isNullOrEmpty())
                    .setText(R.id.tvInterestTitle, item.title)
                helper.adapter.setNewData(item.content)
                helper.getView<RecyclerView>(R.id.rvInterest).scrollBy(-10000, 0)
            }
            1 -> {
                helper.itemView.isSelected = item.expand
                helper.setText(
                    R.id.tvInterestFooter,
                    if (item.expand) R.string.click_to_close else R.string.click_to_pull
                )
            }
        }
    }
}

class InterestViewHolder(view: View) : BaseViewHolder(view) {
    val adapter = InterestItemAdapter()
}

class InterestItemAdapter : BaseQuickAdapter<InterestChildEntity, BaseViewHolder>(R.layout.item_interest_horizontal) {
    override fun convert(helper: BaseViewHolder, item: InterestChildEntity) {
        helper.setText(R.id.tvInterestTitle, item.title).setText(R.id.tvInterestDesc, item.desc)
            .setGone(R.id.tvInterestDesc, item.desc.isNullOrEmpty())
        ImageUtils.loadCircleImage(context, helper.getView(R.id.ivInterest), item.image ?: "")
    }
}